# -*- coding: utf-8 -*-

import warnings

import pandas as pd

from ..base import CommonFunction


class NullHandler(CommonFunction):
    def dropna(self, axis=0, null_value=None):
        if null_value is None:
            self._data.dropna(axis=axis, how='any', inplace=True)
        else:
            if axis == 1:
                for idx, data in self._data.iteritems():
                    for val in self._data.loc[:, idx]:
                        if val == null_value:
                            self._data.drop(idx, axis=1, inplace=True)
                            break
            else:
                for idx, data in self._data.iterrows():
                    for val in self._data.loc[idx, :]:
                        if val == null_value:
                            self._data.drop(idx, axis=0, inplace=True)
                            break

    def fillna(self, axis=0, method='mean', null_value="", value=None):
        if null_value != "":
            self._data.replace(null_value, "")

        if method == 'mean':
            if axis == 1:
                for idx, data in self._data.iteritems():
                    self._data.loc[:, idx] = self._data.loc[:, idx].fillna(self._data[idx].mean())
            else:
                for idx, row in self._data.iterrows():
                    self._data.loc[idx, :] = self._data.loc[idx, :].fillna(self._data.loc[idx, :].mean())
        elif method == 'median':
            if axis == 1:
                for idx, data in self._data.iteritems():
                    self._data.loc[:, idx] = self._data.loc[:, idx].fillna(self._data.loc[:, idx].median())
            else:
                for idx, row in self._data.iterrows():
                    self._data.loc[idx, :] = self._data.loc[idx, :].fillna(self._data.loc[idx, :].median())
        elif method == 'ffill':
            self._data = self._data.fillna(axis=axis, method='ffill')
        elif method == 'bfill':
            self._data = self._data.fillna(axis=axis, method='bfill')
        elif method == 'const':
            self._data = self._data.fillna(axis=axis, value=value)
        else:
            msg = 'method shouldn\'t be ' + str(method)
            raise RuntimeError(msg)

    def predictna(self, method='kmeans', null_value='', attribute=None):
        if null_value != "":
            self._data.replace(null_value, "")
        # TODO
        Y = self._data[attribute]
        X = self._data[attribute]

        if method == 'kmeans':
            pass
        elif method == 'lr':
            pass
        elif method == 'rf':
            pass
        else:
            msg = 'method shouldn\'t be ' + str(method)
            raise RuntimeError(msg)

    def discretizatena(self):
        # TODO
        pass
